### ROBUSTNESS EVALUATION

cat("Figure 7: histogram of t-stats")
data <- read.csv("data.csv") 

t <- paste("nelda", c(3, 4, 5, 11, 13, 15, 16), sep="")
Y <- data[, t]

reregress <- function(x){
data$manip <- apply(Y[,c(1, 2, x)], 1, sum, na.rm=TRUE)
fit <- clmm(as.factor(manip) ~ I(growthc_t1 < -1) + lambda + I(s18f1_lag > 0) + gdp.gle_t1 + agedem_t1 + nelda19 + legelec + poly(year, 3) + (1|country), data=data, link="probit")
b <- coefficients(fit)
t <- which(grepl("growthc|lambda|s18f1", names(b)))
b[t]/sqrt(diag(vcov(fit)[t,t]))
}

tstat=NULL
for (n in 1:5){
  combins <- combn(3:7,n)
  t.t <- apply(combins, 2, reregress)
  tstat <- cbind(tstat,t.t)
} 

pdata <- data.frame(variable = rep(c("Economic crisis", "Coup threat", "Protest"), each = ncol(tstat)), tstat = c(t(tstat)))

pdata$variable <- factor(pdata$variable, levels = c("Economic crisis", "Coup threat", "Protest"))

ggplot(pdata, aes(x=tstat)) + geom_bar(binwidth = 1/6, colour="grey", fill="black") + facet_grid(. ~ variable) + geom_vline(aes(xintercept=-1.96), linetype="solid", color="red", size=1)  + theme_bw() + theme(
  axis.ticks = element_blank(),       
  axis.title.x = element_text(size = 15, face = "bold"),
  strip.text.x = element_text(size = 18, colour = "black", face =  "bold"),
  axis.text.x=element_text(size=12),
  panel.margin = unit(2, "lines")) + labs(x="t-statistics", y="Frequency") + scale_x_continuous(breaks = seq(-6, 1, by = 1))
ggsave("../graphs/robust_histogram.pdf", width=13, height=4)

### Additional analyses
Model <- mirt ~ I(growthc_t1 < -1) + lambda + I(s18f1_lag > 0) + gdp.gle_t1 + agedem_t1 + nelda19 + legelec + poly(year, 3) + (1|country)

### PANEL A
fit.qed <- clmm(update(Model, as.factor(sr11cheat) ~ . - poly(year, 3) + poly(year, 1)), data = data, link="probit") 
vars <- c("I(growthc_t1 < -1)TRUE", "lambda", "I(s18f1_lag > 0)TRUE")
fit.qed <- c(t(cbind(coefficients(fit.qed)[vars], confint(fit.qed, parm=vars))), round(nrow(fit.qed$model), 1))
fit.qed

# PANEL B

# B1: Lagged growth
f.growth  <- lmer(update(Model, . ~ growthc_t1 + . - I(growthc_t1 < -1)), data = data)
f.nelda18 <- update(f.growth, . ~ nelda18 + . - growthc_t1)
f.inflation <- update(f.growth, . ~ I(inflationc_t1 > 1) + . - growthc_t1)

## PANEL C

f.age0 <- lmer(update(Model, . ~ . - agedem_t1), data = data, subset = agedem_t1==0)
f.polity <- lmer(Model, data = data, subset = polity_t1<=5)
f.notran <- lmer(Model, data=data, subset = nelda10==0)
f.nocoup <- lmer(Model, data=data, subset = !(surv.coup<=24 & coupspell > 1))

out <- function(fit) c(t(cbind(fixef(fit)[2:4], confint(fit, parm=4:6))), round(nrow(fit@frame), 1))

names <- c("A.1 QED measure", "B.1 Growth Rate", "B.2 Crisis (NELDA)", "B.3 Inflation shock", "C.1 Autocracies only", "C.2 Polity IV $ \\leq 5$", "C.3 No transitional els.", "C.4 No post-coup els.")

list <- list(growth = f.growth, nelda = f.nelda18 , inflation = f.inflation, autocracy = f.age0, polity=f.polity, notran = f.notran, nocoup = f.nocoup)
list <- lapply(list, out)
list <- do.call("rbind", list)
list <- rbind(fit.qed, list)
list[,1:9] <- roundr(list[, 1:9], 2)
list <- cbind(names, list)
list <- cbind(list[,1], list[,2], paste("(", list[, 3], ",", list[,4], ")", sep = ""), list[,5], paste("(", list[, 6], ",", list[,7], ")", sep = ""), list[,8], paste("(", list[, 9], ",", list[,10], ")", sep = ""), list[,11])


print(xtable(list, 
             align = c("l", "l@{\\hskip.5cm}", "d@{\\hskip-.3cm}", "d@{\\hskip1.25cm}", "d@{\\hskip-.3cm}", "d@{\\hskip1.25cm}", "d@{\\hskip-.3cm}", "d@{\\hskip1cm}", "c"),
             caption = "Testing robustness and alternative explanations.",
             label = "tab:robust",
             table.placement = 't!'
             ),
      size = "small",
      sanitize.text.function = identity,
      hline.after = NULL,
      include.rownames = FALSE,
      include.colnames = FALSE,
      add.to.row = list(pos = list(-1, 0, 0, 1, 4, nrow(list), nrow(list)),
                    command = c("\\toprule\n", "\\multicolumn{1}{l}{} &\\multicolumn{2}{c}{{\\bf Crisis~~~~~~~~}} & \\multicolumn{2}{c}{{\\bf Coup threat~~~~}} & \\multicolumn{2}{c}{{\\bf Protest}} & $N$\\\\ \\midrule \n",
"\\multicolumn{4}{l}{\\emph{Alternative measure of manipulation}}\n \\ML\n", 
"\\midrule \n \\multicolumn{4}{l}{\\emph{Alternative measures of economic crises}}\n \\ML\n",
"\\midrule\n \\multicolumn{4}{l}{\\emph{Alternative subsets of data}}\\ML\n", "\\bottomrule\n", "\\multicolumn{5}{l}{{\\footnotesize Note: 95 percent profile confidence intervals in parentheses.\n}}")), 
file = "../tables/table4.tex")

